package com.aaa.controller;

import com.aaa.dao.MenuDao;
import com.aaa.pojo.*;
import com.aaa.service.*;
import com.aaa.service.impl.SimpleCharVerifyCodeGenImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @Resource
    private PasswordEncoder passwordEncoder;

    private static String code;

    @RequestMapping("/login")
    public String Login(){
        return "login";
    }

    @RequestMapping("/logout")
    public String Logout(){
        return "logout";
    }

    @RequestMapping("/verify")
    @ResponseBody
    public Map<String,String> Verify(ModelMap modelMap){
        System.out.println("传给前端"+code);
        Map<String,String> map = new HashMap<String, String>();
        map.put("code",code);
        return map;
    }

    @RequestMapping("/user")
    @ResponseBody
    public Map<String,String> User(UserInfo userInfo,HttpServletRequest request){
        Map<String,String> map = new HashMap<String, String>();
        if(userInfo.getUsername()!=null & userService.loadUserByUsername(userInfo.getUsername())!=null){
            UserInfo user = userService.loadUserByUsername(userInfo.getUsername());
            map.put("name",user.getUsername());
//            uid = user.getUid();
            request.getSession().setAttribute("uid",user.getUid());
        }
        else{
            map.put("result","false");
        }
        return map;
    }


    @RequestMapping("/test")
    public String Test(){
        return "test";
    }

    @RequestMapping("/register")
    public String Register() {
        return "register";
    }

    @RequestMapping(value = "/reg_info")
    public String Reg_info(UserInfo userInfo){
        String username = userInfo.getUsername();
        String password = userInfo.getPassword();
        String s1 = passwordEncoder.encode(password);
        if(userService.reg_insert(username,s1)){
            return "redirect:login";
        }
        return "register";
    }

    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            code = verifyCode.getCode();

            //将VerifyCode绑定session
            request.getSession().setAttribute("VerifyCode", code);

            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            // LOGGER.info("", e);
            System.out.println(e);
        }
    }

}
